package com.yazi.mxz.common.service.impl;


import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.myframework.smhj.common.exception.MyEntityNotExistsException;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.exception.MyNoPermissionException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;
import com.yazi.mxz.common.db.dao.CompanyDao;
import com.yazi.mxz.common.db.dao.GoodsClassifyDao;
import com.yazi.mxz.common.db.dao.GoodsDao;
import com.yazi.mxz.common.db.dao.StoreGoodsDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.GoodsClassify;
import com.yazi.mxz.common.service.GoodsClassifyService;


@Service("goodsClassifyService")
public class GoodsClassifyServiceImpl extends BasicService<GoodsClassify> implements GoodsClassifyService {
	private static Logger log = LoggerFactory.getLogger(GoodsClassifyServiceImpl.class);
	
	@Resource
	GoodsClassifyDao goodsClassifyDao;
	@Resource
	GoodsDao goodsDao;
	@Resource
	StoreGoodsDao storeGoodsDao;
	@Resource
	CompanyDao companyDao;
	
	@Override
	protected Dao<GoodsClassify> getDao() {
		return goodsClassifyDao;
	}
	
	@Override
	public GoodsClassify addOrEditByCompany(Company company, GoodsClassify classify) throws MyException {
		
		Long classifyId = classify.getClassifyId();
		if (null != classifyId && classifyId > 0) { // 编辑
			
			log.debug("addOrEditByCompany->edit：companyid={},classifyId={}", 
					company.getCompanyId(), classifyId);
			
			GoodsClassify destClassify = goodsClassifyDao.get(classifyId);
			if (null == destClassify) {
				throw new MyEntityNotExistsException();
			}
			if (!destClassify.getCompany().equals(company)) {
				throw new MyNoPermissionException();
			}
			
			// 检查父类
			Long parentId;
			if (null != classify.getGoodsClassify() 
					&& null != (parentId = classify.getGoodsClassify().getClassifyId())
					&& !goodsClassifyDao.get(parentId).getCompany().equals(company)) {
				throw new MyNoPermissionException();
			}
			
			ValueFactory.combine(classify, destClassify);
			destClassify.setUpdateTime(DateUtil.getTimeStamp());
			return goodsClassifyDao.merge(destClassify);
			
		} else { // 添加
			
			log.debug("addOrEditByCompany->add：companyId={},name={}", 
					company.getCompanyId(), classify.getName());
			
			// 数量检查
			if (company.getCatRemain() <= 0) {
				throw new MyNoPermissionException("数量受限");
			}
			company.setCatRemain(company.getCatRemain() - 1);
			companyDao.merge(company);
			
			ValueFactory.validate(classify);
			
			GoodsClassify parent = classify.getGoodsClassify();
			if (null == parent) {
				parent = new GoodsClassify();
				parent.setDeep(-1);
			} else {
				parent = goodsClassifyDao.get(parent.getClassifyId());
				if (!parent.getCompany().equals(company)) {
					throw new MyException();
				}
			}
			classify.setCompany(company);
			classify.setDeep(parent.getDeep() + 1);
			classify.setUpdateTime(DateUtil.getTimeStamp());
			classify.setClassifyId(null);
			return goodsClassifyDao.persist(classify);
		}
		
	}

	@Override
	public void removeByCompany(Company company, long delId, long bakupId) throws MyException {
		log.debug("removeByCompany: companyId={},classifyId={},bakupId={}", 
				new Object[]{company.getCompanyId(), delId, bakupId});
		
		GoodsClassify delClassify = goodsClassifyDao.get(delId);
		// 要删除的类目不存在或者不属于指定企业,则直接路过,不作处理
		if (null == delClassify || !delClassify.getCompany().equals(company)) {
			return;
		}
		
		GoodsClassify bakClassify = goodsClassifyDao.get(bakupId);
		if (null == bakClassify) {
			throw new MyEntityNotExistsException("未指定用来备份商品类目");
		}
		// 备份不属于指定企业,则直接路过,不作处理
		if (!bakClassify.getCompany().equals(company)) {
			return;
		}
		
		// 移动商品
		storeGoodsDao.move(delId, bakupId);
		goodsDao.move(delId, bakupId);
		
		// 删除类目
		int count = goodsClassifyDao.delete(delId);
		
		// 剩余可添加类目的数量
		company.setCatRemain(company.getCatRemain() + count);
		companyDao.merge(company);
		
	}
}
